package com.zxw.设计模式.创建型.建造者模式.建造者模式2;

import cn.hutool.core.util.ObjectUtil;

/**
 * 开源框架应用案例
 *
 * 在MinIO（8.5.4）中创建MinioClient就使用了建造者模式
 *  MinioClient minioClient =  MinioClient.builder().endpoint(endpoint).credentials(accessKey,secretKey).build();
 * 他定义了私有属性、构造方法“添加私有属性”参数、在Builder类中的build方法中返回构造好的MinioClient对象
 * ...
 *  public class MinioClient {
 *      private MinioAsyncClient asyncClient;
 *      ...
 *      public static final class Builder {
 *        public MinioClient build() {
 *             MinioAsyncClient asyncClient = this.asyncClientBuilder.build();
 *             return new MinioClient(asyncClient);
 *         }
 *      ....
 * ....
 *
 * @projectName: util-cloud
 * @package: com.zxw.设计模式.创建型.建造者模式.建造者模式2
 * @className: Bike
 * @author: zhangxuewei
 * @description: 建造模式，用于创建复杂对象
 * @date: 2023/5/16 16:58
 * @version: 1.0
 */
public class Bike {

    private String name;

    private Bike(String name) {
        this.name = name;
    }

    public final static class Builder {
        private String name;

        private String frame;

        private String brake;

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public Builder setFrame(String frame) {
            this.frame = frame;
            return this;
        }

        public Builder setBrake(String brake) {
            this.brake = brake;
            return this;
        }

        public Bike build() {
            // 根据特有属性和规则组件对象
            if (this.name.equals("死飞")) {
                if (ObjectUtil.isNotNull(this.brake))
                    throw new RuntimeException("死飞车型不应该拥有刹车！");
            }
            return new Bike(this.name);
        }

        @Override
        public String toString() {
            return "Builder{" +
                    "name='" + name + '\'' +
                    ", frame='" + frame + '\'' +
                    ", brake='" + brake + '\'' +
                    '}';
        }
    }


    public void music(String msg) {
        //获取 车名怎么获取
        System.out.println(this.name +" 播放：" + msg + "....");
    }

}
